package com.config.dateConfig;


import org.springframework.core.convert.converter.Converter;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateConfig implements Converter<String,Date> {

    @Override
    public Date convert(String source) {
        if (source==null){
            throw new RuntimeException("请输入您传入的数据");
        }
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try{
            return df.parse(source);
        }catch (Exception e){
            throw  new RuntimeException("数据类型转换出错！");
        }
    }


    /*private static final long serialVersionUID = -4580955831439573829L;

    private DateFormat dateFormat;

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public DateConfig(DateFormat dateFormat) {
        this.dateFormat = dateFormat;
    }

    @Override
    public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition) {
        return dateFormat.format(date, toAppendTo, fieldPosition);
    }

    @Override
    public Date parse(String source, ParsePosition pos) {
        Date date = null;
        try {
            date = simpleDateFormat.parse(source, pos);
        } catch (Exception e) {

            date = dateFormat.parse(source, pos);
        }
        return date;
    }

    @Override
    public Date parse(String source) throws ParseException {
        Date date = null;
        try {
            date = simpleDateFormat.parse(source);
        } catch (Exception e) {

            date = dateFormat.parse(source);
        }
        return date;
    }

    @Override
    public Object clone() {
        Object format = dateFormat.clone();
        return new DateConfig((DateFormat) format);
    }*/

}